//
//  ZDMyWalletController.m
//  ZDPension
//
//  Created by 刘甲奇 on 2017/6/19.
//  Copyright © 2017年 maker. All rights reserved.
//

#import "ZDMyWalletController.h"
#import "ZDWalletHeader.h"
#import "ZDWalletCell.h"
#import "MakerTools.h"
#import "ZDOldManModel.h"

static NSString *const ZDWalletCellId = @"ZDWalletCell";

@interface ZDMyWalletController ()

/**
 可用余额
 */
@property(nonatomic, copy) NSString *available_amount;
@end

@implementation ZDMyWalletController

- (void)setupUI
{
    [super setupUI];
    self.title = @"我的钱包";
    
    ZDWalletHeader *headerView = (ZDWalletHeader *)[UIView getNibView:@"ZDWalletHeader"];
    [self.view addSubview:headerView];
    headerView.frame = CGRectMake(0, 0, LEScreenWidth, 80);
    
    [self.tableView registerNib:[UINib nibWithNibName:@"ZDWalletCell" bundle:nil] forCellReuseIdentifier:ZDWalletCellId];
    [self resetTablewFrame:CGRectMake(0, 80, LEScreenWidth, LEScreenHeight -64 -80)];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    
    if (self.isPresent) {
        UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
        self.navigationItem.leftBarButtonItem = leftItem;
    }

    
}
- (void)loadData
{
    [self requestRefreshData];
}
#pragma mark- 网络请求
- (void)requestRefreshData
{

    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    ZDOldManModel *manModel = [MakerTools readFromSandBox:OLDMAN_INFO];
    [dict setObject:manModel.idcard forKey:@"card_id"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:MyWalletUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        ZDLog(@"%s---%d---%@",__func__,__LINE__,successCode);
        NSDictionary *datas = successCode[@"datas"];
        self.available_amount = datas[@"available_amount"];
        NSArray *bills = datas[@"bills"];
        if (bills.count == 0) {
            [SVProgressHUD showInfoWithStatus:@"暂无消费记录"];
            [weakSelf.dataSource removeAllObjects];
        }else
        {
            [weakSelf hiddenNoDataView];
            
            NSArray *tempt = datas[@"bills"];
            NSArray *tempModel = [ZDWalletModel mj_objectArrayWithKeyValuesArray:tempt];
            
            [weakSelf.dataSource removeAllObjects];
            [weakSelf.dataSource addObjectsFromArray:tempModel];
            
        }
        [weakSelf.tableView reloadData];
        
    } falure:^(NSError *er) {
        ZDLog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}

#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZDWalletCell *cell = [tableView dequeueReusableCellWithIdentifier:ZDWalletCellId forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    return cell;
}
#pragma mark- 点击事件
- (void)back
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
